<?php
require_once('header.php');
?>


<div>
	<form action="<?php echo base_url('firstPage/listResult')?>" method="get" name="list">
		<div style="display: table;width: 100%;">
			<div style="float: left"><a href="<?php echo base_url('/firstPage/add');?>">创建文章</a></div>
			<div style="float: right">
				<input type="text" name="title"  placeholder=" 搜索文章" value="<?php echo isset($title)?$title:'';?>"/>
				<input type="hidden" name="searchKey" value="1"/>
				<input type="submit" name="submit" class="btn btn-default" value="搜索"/>
			</div>
		</div>
		<ul class="nav nav-tabs">
			<li role="presentation" class="active"><a href="<?php echo base_url('firstPage/listResult');?>">文章管理</a></li>
			<li role="presentation"><a href="<?php echo base_url('firstPage/userList');?>">用户管理</a></li>
		</ul>
		<?php if($backInfo != ''):?>
			<div class="alert alert-success alert-dismissible" role="alert">
				<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<?php echo $backInfo;?>
			</div>
		<?php endif;?>
		<table class="table">
			<thead>
			<tr>
				<th>#</th>
				<th>标题</th>
				<th>发布时间</th>
				<th>作者</th>
				<th>操作</th>
			</tr>
			</thead>
			<tbody id="list">
			<?php foreach($result as $value):?>
				<tr>
					<td><?php echo $value['eId'];?></td>
					<td><a href="<?php echo base_url("firstPage/show?id={$value['eId']}");?>"><?php echo $value['title'];?></a></td>
					<td><?php echo $value['pTime'];?></td>
					<td><?php echo $value['userId'];?></td>
					<?php if(($username == $value['userId'])||($authority == '管理员')):?>
						<td>
							<a href="/firstPage/add?id=<?php echo $value['eId'];?>">修改</a>
							<a href="/firstPage/del?id=<?php echo $value['eId']?>" onclick="return confirm('确定删除?')?true:false;">删除</a>
						</td>
					<?php else:?>
						<td></td>
					<?php endif;?>
				</tr>
			<?php endforeach;?>
			</tbody>
		</table>
		<?php if(isset($error)):?>
		<div class="alert alert-warning alert-dismissible" role="alert">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			<?php echo $error;?>
		</div>
		<?php endif;?>
	</form>
</div>

<nav>
	<ul class="pagination">
		<?php echo $pag;?>
	</ul>
</nav>

<script type="text/javascript">
	$("document").ready(function(){
		function clickBind(){
			$(".ajax_page").unbind('click').bind('click',function(event){
				var url = $(this).attr("href");
				var p   = url.charAt(url.length - 1); // 获得页码
				if(p>1){
					$.get(url,{},function(res){
						res = eval(res);

						//$("body").html(res); // 全部覆盖
						$("#list").html('');   // 清空列表

						for(var x=0,len = res.length; x<len; x++){
							var txt = "<tr>";
							txt += "<td>"+ res[x]['eId'] +"</td>";
							txt += "<td><a href='"+res[x]['url']+"'>"+ res[x]['title']+"</a></td>";
							txt += "<td>"+ res[x]['pTime']+"</td>";
							txt += "<td>"+ res[x]['userId']+"</td>";
							if(res[x]['givePower']==1){
								txt += "<td>" +
								"<a href='/firstPage/add?id="+res[x]['eId']+"'>修改</a>"+
								"  <a href='/firstPage/del?id="+res[x]['eId']+"' onclick=judge()>删除</a>"
								+"</td>";
							}else{
								txt += "<td></td>";
							}
							txt += "</tr>";
							$("#list").append(txt);
							$(".pagination").html(res[x]['pag']);
						}
						clickBind();
					});
					event.preventDefault();
				}
			});
		}
		clickBind();
		function judge()
		{
			return confirm('确定删除?')?true:false;
		}
	});
</script>
<?php include('foot.php');?>
